package ping.mp3player.service;

import ping.download.HttpDownloader;
import ping.model.Mp3Info;
import ping.mp3player.Mp3ListActivity;
import ping.mp3player.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class DownloadService extends Service {

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Mp3Info mp3Info = (Mp3Info) intent.getSerializableExtra("mp3info");
		//System.out.println("service--->"+mp3Info);
		// TODO Auto-generated method stub
		DownloadThread downloadTread = new DownloadThread(mp3Info);
		Thread thread = new Thread(downloadTread);
		thread.start();		
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	class DownloadThread implements Runnable{
		private Mp3Info mp3Info = null;
		public DownloadThread(Mp3Info mp3Info){
			this.mp3Info = mp3Info;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			String webUrl = "http://192.168.1.7/mp3/";
			String strMp3Name = webUrl + mp3Info.getMp3Name();
			HttpDownloader httpDownloader = new HttpDownloader();
			int result = httpDownloader.downFile(strMp3Name, "mp3/", mp3Info.getMp3Name());
			//String strLrc = httpDownloader.download(webUrl + mp3Info.getLrcName());
			int res = httpDownloader.downFile(webUrl + mp3Info.getLrcName(), "mp3/", mp3Info.getLrcName());
			String resultMsg = null;
			if(result == -1){
				resultMsg  = "下载失败";				
			} else if(result == 0){
				resultMsg = "文件存在无需下载";
			} else if(result == 1){
				resultMsg = "文件下载成功";
			}
			//使用Notification 提示用户下载结果。
//			NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//			Notification notification = new Notification();
//			notification.tickerText = resultMsg;
//			notificationManager.notify(R.drawable.icon, notification);
		}
		
	}

}
